﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Compiler;


namespace GUILibrary
{
    public class Button
    {
        /// <summary>
        /// The default image that the button displays.  Assumed to be the same size as the mouseOver texture.
        /// </summary>
        private Texture2D defaultImage;
        private string defaultImageName;

        /// <summary>
        /// The image that gets displayed when you hover over the button.
        /// Assumed to be the same size as the defaultImage texture.
        /// </summary>
        private Texture2D mouseOver;
        private string mouseOverName;

        private Vector2 position;
        private string action;

        private bool isHovered;

        public string DefaultImageName
        {
            get { return defaultImageName; }
            set { defaultImageName = value; }
        }

        [ContentSerializerIgnore]
        public bool Hovered
        {
            get { return isHovered; }
            set { isHovered = value; }
        }

        public string MouseOverName
        {
            get { return mouseOverName; }
            set { mouseOverName = value; }
        }

        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }

        public virtual int Width
        {
            get { return defaultImage.Bounds.Width; }
        }

        public virtual int Height
        {
            get { return defaultImage.Bounds.Height; }
        }

        public virtual Rectangle Bounds
        {
            get 
            { 
                Rectangle returnMe = defaultImage.Bounds; //NOTE We are directly modifying the defaultImage Bounds instead of copying
                returnMe.X = (int)Position.X;
                returnMe.Y = (int)Position.Y; //NOTE: returnMe's corner coords are not updated
                return returnMe;
            }
        }

        public string Action
        {
            get { return action; }
            set { action = value; }
        }

        public virtual void load(ContentManager cm)
        {
            defaultImage = cm.Load<Texture2D>(defaultImageName);
            mouseOver = cm.Load<Texture2D>(mouseOverName);
        }

        public virtual void Draw(SpriteBatch batch)
        {
            if (Hovered)
                batch.Draw(mouseOver, Position, Color.White);
            else
                batch.Draw(defaultImage, Position, Color.White);
        }
    }
    public class ButtonContentReader : ContentTypeReader<Button>
    {
        protected override Button Read(ContentReader input, Button existingInstance)
        {
            Button button = new Button();
            button.DefaultImageName = input.ReadString();
            button.MouseOverName = input.ReadString();
            button.Position = input.ReadVector2();
            button.Action = input.ReadString();

            button.load(input.ContentManager);

            return button;
        }
    }
}